﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Reflection;
using SN.Models;
using SN.Data;
using SN.Services;

namespace SN.Report
{
    public partial class MainForm : DockContent
    {
        string ShowMsg = "不是编辑窗口!";



        public int FormCount = 1;//新增报表个数

        public FormLeft FrmLeft;
        //查找文档
        private IDockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    if (form.Text == text)
                        return form as IDockContent;

                return null;
            }
            else
            {
                foreach (IDockContent content in dockPanel.Documents)
                    if (content.DockHandler.TabText == text)
                        return content;

                return null;
            }
        }
        //创建文档
        private FormTemp CreateNewDocument(string filename = "")
        {
            FormTemp dummyDoc = new FormTemp();

            int count = 1;
            string text = filename;//+ count.ToString();
            while (FindDocument(text) != null)
            {
                count++;
                text = filename;//;+ count.ToString();
            }
            dummyDoc.Text = text;
            return dummyDoc;
        }

        public MainForm()
        {
            InitializeComponent();
            statusStrip.Items[1].Text = SNService.GetConnectServer();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Assembly asm = Assembly.GetExecutingAssembly();//如果是当前程序集
            this.Text = SNService.GetAppTitle(asm);
            //登陆窗体
            FormLogin Login = new FormLogin();
            if (Login.ShowDialog() == DialogResult.OK)
            {
                //开始加载别的
                FrmLeft = new FormLeft();
                FrmLeft.Mdi = this; //Mdi 
                FrmLeft.MdiParent = this;
                FrmLeft.Show(this.dockPanel);
                FrmLeft.DockTo(this.dockPanel, DockStyle.Left);

                FormRight FrmRight = new FormRight();
                FrmRight.Show(this.dockPanel);
                FrmRight.DockTo(this.dockPanel, DockStyle.Fill);
            }
            else
            {
                Login.Close();
                Application.Exit(); //关闭程序
            }


        }
        private void menuItemClose_Click(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
                ActiveMdiChild.Close();
            else if (dockPanel.ActiveDocument != null)
                dockPanel.ActiveDocument.DockHandler.Close();
        }

        private void menuItemCloseAll_Click(object sender, EventArgs e)
        {
            //关闭所有功能
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    form.Close();
            }
            else
            {
                for (int index = dockPanel.Contents.Count - 1; index >= 0; index--)
                {
                    if (dockPanel.Contents[index] is IDockContent)
                    {
                        IDockContent content = (IDockContent)dockPanel.Contents[index];
                        content.DockHandler.Close();
                    }
                }
            }
        }

        private void menuItemCloseAllButThisOne_Click(object sender, EventArgs e)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                Form activeMdi = ActiveMdiChild;
                foreach (Form form in MdiChildren)
                {
                    if (form != activeMdi)
                        form.Close();
                }
            }
            else
            {
                foreach (IDockContent document in dockPanel.DocumentsToArray())
                {
                    if (!document.DockHandler.IsActivated)
                        document.DockHandler.Close();
                }
            }
        }
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAdd frmAdd = new FormAdd();
            if (frmAdd.ShowDialog() != DialogResult.OK) return;

            FormTemp dummyDoc = CreateNewDocument(frmAdd.Report.Report_Name);


            dummyDoc.Report = frmAdd.Report;//将新增的类赋值
            //给树结点
            //
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                dummyDoc.MdiParent = this;
                dummyDoc.Show();
            }
            else
            {
                dummyDoc.Show(dockPanel);
            }
            frmAdd.Dispose();//手动销毁
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.HideOnClose = false;
            try
            {
                this.Dispose();
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }


        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Msg = string.Empty;
            Form frm = this.ActiveMdiChild; //当前活动窗体
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                //调用该窗体的写文件的方法
                Msg = frmActinve.WriteBinaryData();
                if (!string.IsNullOrEmpty(Msg))
                {
                    MessageBox.Show(Msg);
                }
            }
        }

        private void 导入Xls文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                //调用该窗体的写文件的方法
                frmActinve.cellControl1.ImportExcelDlg();//读取文件
            }
            //FormTemp dummyDoc = CreateNewDocument();
            ////赋值
            //SN.Models.T_Report report = new T_Report();
            //report.Report_Name = dummyDoc.Text;
            //dummyDoc.Report = report;

            //dummyDoc.cellControl.ImportExcelDlg(); //读取文件
            //if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            //{
            //    dummyDoc.MdiParent = this;
            //    dummyDoc.Show();
            //}
            //else
            //    dummyDoc.Show(dockPanel);
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                frmActinve.cellControl1.axCell.PrintSheet(1, frmActinve.cellControl1.axCell.GetCurSheet());
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Msg = string.Empty;
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                //
                //
                if (MessageBox.Show("你要删除报表模块:" + frmActinve.Report.Report_Name, "提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;

                frmActinve.DeleteReport(ref Msg);//删除指标数据
                if (!string.IsNullOrWhiteSpace(Msg))
                {
                    MessageBox.Show(Msg);
                }
                else
                {
                    //删除成功
                    frmActinve.Close();
                    MessageBox.Show("删除成功，请刷新数据!");
                }
            }
        }
        private void 重命名RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Msg = string.Empty;
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormRename fRename = new FormRename();
                if (fRename.ShowDialog() != DialogResult.OK) return;

                FormTemp frmActinve = (FormTemp)frm;
                //
                Msg = frmActinve.RenameReport(fRename.SnewName);
                if (!string.IsNullOrEmpty(Msg))
                {
                    MessageBox.Show(Msg);
                }
                else
                {
                    frmActinve.Text = fRename.SnewName; //新命名的
                    MessageBox.Show("更新成功!请刷新数据...");
                }
            }
        }

        private void 导出Xls文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                //调用该窗体的写文件的方法
                frmActinve.cellControl1.ExportExcelDlg();//导出文件
            }
        }

        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm == null) return;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                frmActinve.cellControl1.axCell.PrintPreview(1, frmActinve.cellControl1.axCell.GetCurSheet());
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm.Name.ToUpper() != "FORMTEMP")
            {
                MessageBox.Show(ShowMsg);
                return;
            }
            else
            {
                FormTemp frmActinve = (FormTemp)frm;
                frmActinve.cellControl1.axCell.PrintPageSetup(); //页面设置

            }
        }

        private void 功能导航ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FrmLeft.Visible == false)
            {
                FrmLeft = new FormLeft();
                FrmLeft.Mdi = this; //Mdi 
                FrmLeft.MdiParent = this;
                FrmLeft.Show(this.dockPanel);
                FrmLeft.DockTo(this.dockPanel, DockStyle.Left);
            }
        }

    }

}